#ifndef FORMAT_H
#define FORMAT_H

// 定义类型和宏定义
typedef unsigned char u_char; // 1个字节
typedef unsigned short u_short; // 2个字节
typedef unsigned int u_int; // 4个字节
typedef unsigned long u_long; // 4个字节

// 以太网协议格式
/*
+-------------------+-----------------+------+
|       6 byte      |     6 byte      |2 byte|
+-------------------+-----------------+------+
|destination address|  source address | type |
+-------------------+-----------------+------+
*/

typedef struct ether_header{ // 14个字节
    u_char ethernet_des_host[6]; // 目的地址[6个字节]
    u_char ethernet_src_host[6]; // 源地址[6个字节]
    u_short type; // 类型[2个字节]
}ETHER_HEADER;

// Ip的头部
/*
+-------+-----------+---------------+-------------------------+
| 4 bit |   4 bit   |    8 bit      |          16 bit         |
+-------+-----------+---------------+-------------------------+
|version|head length|  TOS/DS_byte  |        total length     |
+-------------------+--+---+---+----+-+-+-+-------------------+
|          identification           |R|D|M|    offset         |
+-------------------+---------------+-+-+-+-------------------+
|       ttl         |     protocal  |         checksum        |
+-------------------+---------------+-------------------------+
|                   source ip address                         |
+-------------------------------------------------------------+
|                 destination ip address                      |
+-------------------------------------------------------------+
*/
typedef struct ip_header{ // 20个字节
    u_char version_length; // 版本[4位]和报头长度[4位]
    u_char TOS; // TOS/DS字节[1字节]
    u_short total_length; // IP包总长度[2字节]
    u_short identification; // 标识[2字节]
    u_short offset; // 标志位[3位]和偏移量[13位]
    u_char ttl; // TTL[1字节]
    u_char protocol; // 协议[1字节]
    u_short checksum; // 校验和[2字节]
    u_int src_addr; // 源地址[4字节]
    u_int des_addr; // 目的地址[4字节]

}IP_HEADER;

// Tcp的头部
/*
+----------------------+---------------------+
|         16 bit       |       16 bit        |
+----------------------+---------------------+
|      source port     |  destination port   |
+----------------------+---------------------+
|              sequence number               |
+----------------------+---------------------+
|                 ack number                 |
+----+---------+-------+---------------------+
|head| reserve | flags |     window size     |
+----+---------+-------+---------------------+
|     checksum         |   urgent pointer    |
+----------------------+---------------------+
*/

typedef struct tcp_header{ // 20个字节
    u_short src_port; // 源端口[2字节]
    u_short des_port; // 目的[2字节]
    u_int seqequence; // 序号[4字节]
    u_int ack; // 确认序列号[4字节]
    u_char header_length; // 报头长度[4位]
    u_char flags; // 标志[6位]
    u_short window_size; // 窗口大小[2字节]
    u_short checksum; // 校验和[2字节]
    u_short urgent; // 紧急指针[2字节]

}TCP_HEADER;

// Udp的头部
/*
+---------------------+---------------------+
|        16 bit       |        16 bit       |
+---------------------+---------------------+
|    source port      |   destination port  |
+---------------------+---------------------+
| data package length |       checksum      |
+---------------------+---------------------+
*/

typedef struct udp_header{ // 8个字节
    u_short src_port; // 源端口[2字节]
    u_short des_port; // 目的端口[2字节]
    u_short data_length; // 数据长度[2字节]
    u_short checksum; // 校验和[2字节]
}UDP_HEADER;

//Arp
/*
|<--------  ARP header  ------------>|
+------+--------+-----+------+-------+----------+---------+---------------+--------------+
|2 byte| 2 byte |1byte| 1byte|2 byte |  6 byte  | 4 byte  |     6 byte    |     4 byte   |
+------+--------+-----+------+-------+----------+---------+---------------+--------------+
| type |protocol|e_len|ip_len|op_type|source mac|source ip|destination mac|destination ip|
+------+--------+-----+------+-------+----------+---------+---------------+--------------+
*/

typedef struct arp_header{ // 28个字节
    u_short type; // 硬件类型[2字节]
    u_short protocol; // 协议[2字节]
    u_char mac_length; // MAC地址长度[1字节]
    u_char ip_length; // IP地址长度[1字节]
    u_short op_code; // 操作码[2字节]

    u_char src_eth_addr[6]; // 源以太地址[6字节]
    u_char src_ip_addr[4]; // 源IP地址[4字节]
    u_char des_eth_addr[6]; // 目的以太地址[6字节]
    u_char des_ip_addr[4]; // 目的IP地址[4字节]
}ARP_HEADER;

// Icmp的头部
/*
+---------------------+---------------------+
|  1 byte  |  1 byte  |        2 byte       |
+---------------------+---------------------+
|   type   |   code   |       checksum      |
+---------------------+---------------------+
|    identification   |       sequence      |
+---------------------+---------------------+
|                  option                   |
+-------------------------------------------+
*/

typedef struct icmp_header{ // 至少8个字节
    u_char type; // 类型[1字节]
    u_char code; // 代码[1字节]
    u_short checksum; //校验和[2字节]
    u_short identification; //标识[2字节]
    u_short sequence; // 序列[2字节]
}ICMP_HEADER;

// dns
/*
+--------------------------+---------------------------+
|           16 bit         |1b|4bit|1b|1b|1b|1b|3b|4bit|
+--------------------------+--+----+--+--+--+--+--+----+
|      identification      |QR| OP |AA|TC|RD|RA|..|Resp|
+--------------------------+--+----+--+--+--+--+--+----+
|         Question         |       Answer RRs          |
+--------------------------+---------------------------+
|     Authority RRs        |      Additional RRs       |
+--------------------------+---------------------------+
*/

typedef struct dns_header{ // 12个字节
   u_short identification; // 标识[2个字节]
   u_short flags; // 标志[2个字节]
   u_short question; // 问题编号[2个字节]
   u_short answer; // 应答RRS[2个字节]
   u_short authority; // 权限[2个字节]
   u_short additional; // 附加[2个字节]
}DNS_HEADER;

// dns question
typedef struct dns_question{
    // char* name;          // Non-fixed
    u_short query_type;     // 2 byte
    u_short query_class;    // 2 byte
}DNS_QUESTION;

typedef struct dns_answer{
    // char* name          // Non-fixed
    u_short answer_type;   // 2 byte
    u_short answer_class;  // 2 byte
    u_int TTL;             // 4 byte
    u_short dataLength;    // 2 byte
    //char* name           // Non-fixed
}DNS_ANSWER;

#endif // FORMAT_H
